//====================================================//
//========Eric Muehlberg==============================//
//====================================================//
//========CS197C Puzzle game==========================//
//====================================================//

//SDL header includes
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
//Engine header includes
#include "Timer.h"
#include "engine.h"
//Object includes
#include "Player.h"
#include "Block.h"
#include "course.h"
#include "Stage.h"
//IO and string includes
#include <string>
#include <sstream>
#include <iostream>
#include <map>

int main(int argc, char* args[])
{
	engine e = engine();
	std::string coursefile = "course";


	bool literals = false;
	while(literals == false)
	{
		std::cout << R"(Are you a fan of "Jackie Chan Adventures"?)" << std::endl;
		std::string answer;
		/*try
		{
			std::cin >> answer;
			throw(answer);
		}
		catch(answerno e)
		{
			std::cout << R"(You're missing out)" << std::endl;
		}
		catch(answeryes e)
		{

		}*/

		literals = true;
	}

	int i = 0;
	std::map<int,char> m = std::map<int, char>();
	std::map<int,char>::iterator iter = m.begin();

	while(i < 10)
	{
		m.insert(iter, std::pair<int,char>(i,((char)i)));
		i++;
	}

	iter = m.begin();
	for(;iter != m.end(); iter++)
	{
		std::cout << (*iter).first << " " << (*iter).second << std::endl;
	}

	if(e.Init() == false)
		return 1;

	Stage stage1 = Stage();

	e.background = e.load_image("background.png");
	Player p = Player(0, 0, 50, 50, e.load_image("player.png"));

	course c = course();

	if(c.loadcourse(coursefile, e) == false)
		return -1;


	Timer fps = Timer();

	fps.Start();

	while(e.quit == false)
	{
		//Handle input

		while(SDL_PollEvent(&e.event))
		{
			if(e.event.type == SDL_QUIT)
			{
				e.quit = true;
			}
			Uint8 *keystate = SDL_GetKeyState(NULL);

			if(p.canmove == true)
			{
				if(keystate[SDLK_UP])
				{
					p.setDy(-200);
					p.canmove = false;
				}
				if(keystate[SDLK_DOWN])
				{
					p.setDy(200);
					p.canmove = false;
				}
				if(keystate[SDLK_LEFT])
				{
					p.setDx(-200);
					p.canmove = false;
				}
				if(keystate[SDLK_RIGHT])
				{
					p.setDx(200);
					p.canmove = false;
				}
			}
				if(keystate[SDLK_q])
				{
					e.quit = true;
				}
				if(keystate[SDLK_r])
				{
					/*p.sprite.x = c.getStage().startx;
					p.sprite.y = c.getStage().starty;*/
				}
		}

		//Update player object
		p.Update(fps.get_ticks());
		//Not necessary to update other objects
		/*c.Update(fps.get_ticks());*/

		fps.Start();

		//Check collisions with all objects
		//For each loop for objects
		for(Block b: stage1.getBlocks())
		{
			if(e.CollisionCheck(p.sprite.CollBox, b.sprite.CollBox))
			{
				p.setDx(0);
				p.setDy(0);
				p.canmove = true;
			}
		}

		//Check to see if stage has ended
		if(p.canmove)
		{
			if(e.CollisionCheck(p.sprite.CollBox, /*c.getStage().stageend*/stage1.stageend.sprite.CollBox))
			{
				//c.nextStage();
			}
		}

		//Update screen
		e.apply_surface(0,0,e.background, e.screen, NULL);

		for(Block b: stage1.getBlocks())
		{
			e.apply_surface(b.sprite.x, b.sprite.y, b.sprite.sprite, e.screen, NULL);
		}

		e.apply_surface(stage1.stageend.sprite.x, stage1.stageend.sprite.y, stage1.stageend.sprite.sprite, e.screen, NULL);

		e.apply_surface(p.sprite.x, p.sprite.y, p.sprite.sprite, e.screen, NULL);

		if(e.screen != NULL)
			SDL_Flip(e.screen);
	}

	Mix_HaltMusic();

	SDL_FreeSurface(e.background);
	Mix_FreeMusic(e.audio);

	Mix_CloseAudio();
	SDL_Quit();

	return 0;
}
